<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /**
       * @param {number} x
       * @return {number}
       */
      var reverse = function (x) {
        // 中转变量
        var temp = "";
        // 判断是否正数
        var isPositive = true;
        if (x < 0) {
          isPositive = false;
          x = Math.abs(x) + "";
        } else {
          x = x + "";
        }

        for (var i = x.length - 1; i >= 0; i--) {
          temp += x.charAt(i);
        }
        temp = parseInt(temp);
        if (temp >= -(2 ** 31) && temp < 2 ** 31) {
          return isPositive ? temp : -temp;
        }

        return 0;
      };

      var x = -123;
      console.log(reverse(x));
    </script>
  </body>
</html>
